/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package service

import (
	"context"
	"gitee.com/WisdomClassroom/core"

	"gitee.com/WisdomClassroom/core/models"
	"gitee.com/WisdomClassroom/core/protobufs/v1/pb"
	"gitee.com/WisdomClassroom/question/glb"
)

func (s *Service) GetQuestions(ctx context.Context, request *pb.GetQuestionsRequest) (*pb.GetQuestionsResponse, error) {
	questions := make([]models.Question, 0, len(request.GetQuestionUUIDs()))
	err := glb.DB.Find(&questions, "uuid in (?)", request.GetQuestionUUIDs()).Error
	if err != nil {
		glb.Logger.Error(err.Error())
		return getQuestionsRespSvrErr, nil
	}

	questionsResp := make([]*pb.QuestionBasicInfo, 0, len(request.GetQuestionUUIDs()))
	for _, question := range questions {
		questionsResp = append(questionsResp, &pb.QuestionBasicInfo{
			UUID:     question.UUID,
			Question: question.Question.String,
			Answers: &pb.QuestionAnswers{
				AnswerA: question.AnswerA.String,
				AnswerB: question.AnswerB.String,
				AnswerC: question.AnswerC.String,
				AnswerD: question.AnswerD.String,
				AnswerE: question.AnswerE.String,
				AnswerF: question.AnswerF.String,
				AnswerG: question.AnswerG.String,
				AnswerH: question.AnswerH.String,
			},
			Options: &pb.QuestionOptions{
				IsA:    question.IsA.Bool,
				IsB:    question.IsB.Bool,
				IsC:    question.IsC.Bool,
				IsD:    question.IsD.Bool,
				IsE:    question.IsE.Bool,
				IsF:    question.IsF.Bool,
				IsG:    question.IsG.Bool,
				IsH:    question.IsH.Bool,
				IsTrue: question.IsTrue.Bool,
			},
			Analysis: question.Analysis.String,
			Type:     question.Type.Int32,
		})
	}

	return &pb.GetQuestionsResponse{
		Status:    &pb.ResponseStatus{Code: core.ResponseStatusCodeSuccess},
		Questions: questionsResp,
	}, nil
}
